<?php

namespace App\Admin\Controllers;

use Carbon\Carbon;
use App\Models\User;
use App\Models\Order;
use App\Models\Product;
use Encore\Admin\Layout\Row;
use Encore\Admin\Layout\Column;
use Encore\Admin\Layout\Content;
use Encore\Admin\Widgets\InfoBox;
use App\Http\Controllers\Controller;
use Encore\Admin\Controllers\Dashboard;

class HomeController extends Controller
{
    public function index(Content $content)
    {
        $content->header('店铺概况');
        $content->description('又有新的订单，真好...');


        $content->row(function ($row) {
            $row->column(3, new InfoBox('待发货订单', 'truck', 'yellow', '/admin/orders', $this->orderNeedDelivery()));
            $row->column(3, new InfoBox('今日订单数', 'shopping-cart', 'yellow', '/admin/orders', $this->todayOrder()));
            $row->column(3, new InfoBox('订单总数', 'balance-scale', 'yellow', '/admin/orders', $this->totalOrder()));
            $row->column(3, new InfoBox('今日新增用户 ', 'users', 'aqua', '/admin/users', $this->todayUser()));
        });
        $content->row(function ($row) {
            $row->column(3, new InfoBox('今日销售额', 'money', 'green', '/admin/orders', $this->todayMoney()));
            $row->column(3, new InfoBox('销售总额', 'money', 'green', '/admin/orders', $this->todayMoney()));
            $row->column(3, new InfoBox('上架商品', 'cube', 'blue', '/admin/products', $this->productOn()));
            $row->column(3, new InfoBox('商品总数', 'cubes', 'blue', '/admin/products', $this->productTotal()));
        });

        return $content;
    }
    //销售额
    public function todayMoney()
    {
        return Order::where([['paid_at','<>',null],['refund_status','pending'],['created_at','>=',Carbon::today()]])->sum('total_amount');
    }
    public function totalMoney()
    {
        return Order::where([['paid_at','<>',null],['refund_status','pending']])->sum('total_amount');
    }
    //订单
    public function todayOrder()
    {
        return Order::where([['paid_at','<>',null],['created_at','>=',Carbon::today()]])->count();
    }
    public function totalOrder()
    {
        return Order::where([['paid_at','<>',null]])->count();
    }
    public function orderNeedDelivery()
    {
        return Order::where([['paid_at','<>',null],['refund_status','pending'],['ship_status','pending']])->count();
    }
    //用户
    public function todayUser()
    {
        return User::where('created_at','>=',Carbon::today())->count();
    }
    //商品
    public function productOn()
    {
        return Product::where('on_sale',1)->count();
    }
    public function productTotal()
    {
        return Product::count();
    }
}
